<?php
namespace app\index\model;
use think\Model;
use think\Db;
class Experiment extends Model{
    //提交具体数据
    public function commitData(){
        validDo();
        $surveyor=chk(input('surveyor',null),'请填写测量员名称');
        $tool=chk(input('tool',null),'请填写测量工具');
        $thing=chk(input('thing',null),'请填写测量物件');
        $testValue=chk(input('testValue',null),'请填写测量数值');
        $imageUrl=input('imageUrl',null);
        $data['surveyor']=$surveyor;
        $data['tool']=$tool;
        $data['thing']=$thing;
        $data['testValue']=$testValue;
        $data['addTime']=time();
        $data['imageUrl']=$imageUrl;
        $rs=Db::name('data')->insert($data);
        if($rs){
            returnResult(0,'提交成功');
        }else{
            returnResult(1001,'系统异常，稍后重试');
        }
    }
    //获取实验测试数据
    public function dataList(){
       // die;
        // $surveyor=trim(input('surveyor',null));
        // $tool=trim(input('tool',null));
        // $thing=trim(input('thing',null));
        $start_time=strtotime(input('start_time',null));
        $end_time=strtotime(input('end_time',null));
        $searchType=input('searchtype',null);
        $keyword=input('keyword',null);
        $map=array();
        if(!empty($start_time)){
            $map[]=array(['addTime','>',$start_time]);
        
        }
        if(!empty($end_time)){
            $map[]=array(['addTime','<',$end_time]);
          
        }
        if(!empty($keyword)){
        if(empty($searchType)){
          
                $map[]=array('surveyor|thing|tool','=',$keyword);
           
          
        }else if($searchType=='surveyor'){
          $map[]=array('surveyor','=',$keyword);
        }else if($searchType=='tool'){
            $map[]=array('tool','=',$keyword);
        }else if($searchType=='thing'){
            $map[]=array('thing','=',$keyword);
        }
    }
        // if(!empty($surveyor)){
        //     $map['surveyor']=$surveyor;
        // }
        // if(!empty($tool)){
        //     $map['tool']=$tool;
        // }
        // if(!empty($thing)){
        //     $map['thing']=$thing;
        // }
        $dataList=Db::name('Data')->where($map)->order("id DESC")->paginate(10,false,['query' => request()->param()]); 
        //  dump($dataList);
        //  die;
        return $dataList;
    }
    //对操作记录进行删除
    public function del($id){
        $rs=Db::name('Data')->where('id',$id)->delete();
        return $id;
    }

    //获取所有的记录
    public function getRecord(){
        $dataList=Db::name('Data')->order("id DESC")->select();
        return $dataList; 
    }
}
?>